package ar.edu.unlu.sistemas.movirdroid.business.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.os.Environment;

public class AndroidHelper {

	public static int getSDCardState() {

		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
			// We can read and write the media
			return Constants.SD_MOUNTED_WRITE;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			// We can only read the media
			return Constants.SD_MOUNTED_READ;
		} else {
			// Something else is wrong. It may be one of many other states, but all we need
			//  to know is we can neither read nor write
			return Constants.SD_UNMOUNTED;
		}
	}
	
	public static String getipAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        String ipaddress=inetAddress.getHostAddress().toString();
                        Logger.logInfo("SERVICIO", "ip address "+ipaddress);
                        return ipaddress;
                    }
                }
            }
        } catch (SocketException ex) {
            Logger.logError(AndroidHelper.class.getSimpleName(),"Error obteniendo IP de interface", ex);
        }
        return null;
    }
	
}
